package io.github.talelin.sleeve.module.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.Properties;

/**
 * @Classname KafkaProducerTest
 * @Description TODO
 * @Date 2021/8/2 15:21
 * @Created by bld
 */
@Component
public class KafkaProducerTest {

    @Value("${spring.kafka.topics.topic1}")
    private String topic;
    public void pushKafkaTest(String message) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("key.serializer", StringSerializer.class.getName());
        props.put("value.serializer", StringSerializer.class.getName());
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        try {
            producer.send(new ProducerRecord<String, String>(topic,"key", message));
            System.out.println("发送成功");
        } catch (Exception e) {
            System.out.println("发送失败");
            e.printStackTrace();
        }
    }
}
